package com.mycompany.project.client.duch;

import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.mycompany.project.client.Main;

/**
 * This class provides accessors and modifiers for the local storage containing
 * the last time the rover was calibrated.
 * @author Timothy Duch
 */
public class RoverCalibration {
    /**
     * The consistent format for the datetime.
     */
    @SuppressWarnings("deprecation")
    private final transient DateTimeFormat dateformat =
    DateTimeFormat.getFullDateTimeFormat();
    /**
     * The date field containing the last time the rover was calibrated.
     */
    private Date date;

    /**
     * Constructor for the class. Checks for prior existence of date.
     * If it does exist, pulls local data date into date field.
     */
    public RoverCalibration() {
        if (Main.getLocal().getItem("Calibration") != null) {
            date = dateformat.parse(Main.getLocal().getItem("Calibration"));
        }
    }

    /**
     * @return date is the date of the last calibration.
     */
    public final Date getDate() {
        return (Date) date.clone();
    }

    /**
     * @return stringDate is the last calibration date in string format.
     */
    public final String getStringDate() {
        String stringDate;
        if (date == null) {
            stringDate = null;
        } else {
            stringDate = dateformat.format(date);
            }
        return stringDate;
    }

    /**
     * This is the setter for date.
     * @param newDate is the new date of calibration.
     */
    public final void setDate(final Date newDate) {
        date = newDate;
        Main.getLocal().setItem("Calibration", dateformat.format(newDate));
    }

    /**
     * Calculates number of days since last calibration.
     * @return daysElapsed is the number of days between the last calibration
     * and the current date.
     */
    public final int daysElapsed() {
        int daysElapsed = -1;
        if (date != null) {
            daysElapsed = CalendarUtil.getDaysBetween(date, new Date());
        }
        return daysElapsed;
    }

}
